package cn.edu.neu.springbootdemo.action;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import cn.edu.neu.springbootdemo.model.Movie;
import cn.edu.neu.springbootdemo.model.MovieChart;
import cn.edu.neu.springbootdemo.model.Screens;
import cn.edu.neu.springbootdemo.service.*;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.neu.springbootdemo.model.User;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;


@Controller
@RequestMapping("/admin")
public class AdminAction {
    //用户管理
    @Resource(name = "userServiceImpl")
    UserService userService;
    //电影管理
    @Resource(name = "movieServiceImpl")
    private MovieService movieService;
    //放映厅管理
    @Resource(name="screensServiceImpl")
    private ScreensService screensService;
    //放映表
   @Resource(name = "movieChartServiceImpl")
    private MovieChartService movieChartService;

// 用户管理+++++++++++++++++++++++++++++++++++++++++++++
    //用户列表显示列表
    @RequestMapping("/getUserList")
    public String Listuser(Model model) {
        List<User> list = userService.getUserList();
        model.addAttribute("UserList", list);
        return "/admin/home";
    }

    //添加用户
    @RequestMapping("/addUser")
    public String addUser(User user, Model model) {
        userService.addUser(user);
        System.out.println(user+"++++++++++++++++++++++++++++++++++adduser++++++++++++++++++++++++++++++++++++++");
        model.addAttribute("result", "yes");
        model.addAttribute("msg", "添加成功");
        return "/admin/result";

    }

    //查询
    @RequestMapping("/findUser")
    public String findUser(User user, Model model) {
        List<User> user1 = userService.getfindUser(user);
        model.addAttribute("UserList", user1);
        return "/admin/home";
    }

    @RequestMapping("/findIdUser")
    public String findById(User user, Model model) {
        User findUser = userService.findById(user);
        model.addAttribute("userList", findUser);
        return "/admin/updata";
    }
    //修改
    @RequestMapping("/UpdataUser")
    public  String UpdataUser(User user){
        System.out.println(user+"+++++++++++++++11561321");
         userService.upData(user);
        return "/admin/result";
    }

    //删除
    @RequestMapping("/DeleteUser")
    public String DeleteUser(@RequestParam(name = "userids", required = false) String userids) {
        String[] userid = userids.split(",");
        for (String a : userid) {
            Boolean an = userService.deleteUser(Integer.parseInt(a));
        }
        return "forward:/admin/getUserList";
    }
//电影管理++++++++++++++++++++++++++++++++++++++
    //显示电影列表
    @RequestMapping("/showMovie")
    public String showMovie(Model model){
        List<Movie> list=movieService.showMovieList();
        model.addAttribute("MovieList",list);
        return "/admin/showMovie";
    }
    //添加电影
    @RequestMapping("/addMovie")
    public String addMovie(Movie movie){
        //添加图片
        if (movie.getFile().isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = movie.getFile().getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String filepath = System.getProperty("user.dir")+"\\springbootdemo\\src\\main\\resources\\static\\images\\";
        fileName = "Movie"+System.currentTimeMillis() + suffixName;
        File dest = new File(filepath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            movie.getFile().transferTo(dest);
//            System.out.println(dest+"++++++++dest+++++++dest++++++dest++++++");
        } catch (IOException e) {
//            System.out.println("++++++QwQ+++++++");
            e.printStackTrace();
        }
        String filename = "/springbootdemo/images/" + fileName;
//        System.out.println(filename+"++++++++++++++++++++++++++++++++");
        movie.setFilm_pic(filename);

        //添加电影
        movieService.addMovie(movie);
        return "/admin/result";
    }

    @RequestMapping("/deleteMovie")
    public String DeleteMovie(@RequestParam(name = "film_id", required = false) String film_ids, Model model) {
        String[] film_id = film_ids.split(",");
        for (String a : film_id) {
            Boolean an = movieService.deleteMovie(Integer.parseInt(a));
        }
        return "forward:/admin/showMovie";
    }
    @RequestMapping("/findMovie")
    public String findMovie(Movie movie, Model model){
        System.out.println(movie.toString()+"findMovie");
        List<Movie> movielist = movieService.getfindMovie(movie);
        model.addAttribute("MovieList", movielist);
        return "/admin/showMovie";
    }

    @RequestMapping("/findIdMovie")
    public String findIdMovie(Movie movie,Model model){
        Movie findMovie = movieService.findById(movie.getFilm_id());
        model.addAttribute("MovieList",findMovie);
        return "/admin/updataMovie";

    }
    @RequestMapping("/updateMovie")
    public  String updateMovie(Movie movie,Model model){
        movieService.updateMovie(movie);
        model.addAttribute("");
        return "forward:/admin/showMovie";
    }


//放映厅管理++++++++++++++++++++++++++++++++++++++
    @RequestMapping("/showScreens")
    public String showScreens(Model model){
        List<Screens> ScreensList=screensService.ShowScreens();
        model.addAttribute("ScreensList",ScreensList);
        return "/admin/showScreens";
    }

    @RequestMapping("/addScreens")
    public  String addScreens(Screens screens,Model model){
        screensService.addScreens(screens);
        return "/admin/result";
    }

    @RequestMapping("/deleteScreens")
    public  String deleteScreens(@RequestParam(name = "screens_id", required = false) String screens_id, Model model){
        String[] screens_ids = screens_id.split(",");
        for (String a : screens_ids) {
            Boolean an = screensService.deleteService(Integer.parseInt(a));
        }
        return "forward:/admin/showScreens";
    }

    @RequestMapping("/findScreens")
    public String findScreens(Screens screens, Model model){
        List<Screens> Screenslist = screensService.getfindScreens(screens);
        System.out.println(Screenslist+"Screenslist+++++++++++++++++++++++++++++");
        model.addAttribute("ScreensList", Screenslist);
        return "/admin/showScreens";
    }

    @RequestMapping("/findIdScreens")
    public String findIdScreens(Screens screens,Model model){
        Screens findScreens = screensService.findById(screens.getScreens_id());
        model.addAttribute("ScreensList",findScreens);
        return "/admin/updateScreens";
    }


    @RequestMapping("/updateScreens")
    public  String updateScreens(Screens screens,Model model){
        screensService.updateScreens(screens);
        return "forward:/admin/showScreens";
    }
    //放映表管理+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    @RequestMapping("/showMovieChart")
    public  String showChart(Model model){
        List<Screens> ScreensList=screensService.ShowScreens();
        List<MovieChart> MovieChartList= movieChartService.showMovieChart();
        model.addAttribute("MovieChartList",MovieChartList);
        model.addAttribute("ScreensList",ScreensList);
        return "/admin/showMovieChart";
    }

    @RequestMapping("/addChartMovie")
    public  String addChartMovie(MovieChart movieChart,Model model){
        movieChartService.addChartMovie(movieChart);
        return "forward:/admin/showMovieChart";
    }

    @RequestMapping("/deleteChartMovie")
    public String deleteChartMovie(@RequestParam(name = "show_ids", required = false) String show_ids, Model model){
        String[] show_id = show_ids.split(",");
        for (String a : show_id) {
            Boolean an = movieChartService.deleteChartMovie(Integer.parseInt(a));
        }
        return "forward:/admin/showMovieChart";
    }

    @RequestMapping("/findChartMovie")
    public String findChartMovie(MovieChart movieChart,Model model){
//        System.out.println(movieChart+"++++++++++++++++++++++++movieChart++++++++++++++++");
        List<MovieChart>ChartMovielist = movieChartService.getfindChartMovie(movieChart);
        List<Screens> ScreensList=screensService.ShowScreens();

//        System.out.println(ChartMovielist+"+++++++++++++++++ChartMovielist9+++++++++++++++++++++++++++");
        model.addAttribute("MovieChartList", ChartMovielist);
        model.addAttribute("ScreensList",ScreensList);
        return "/admin/showMovieChart";
    }



    @RequestMapping("/findIdChartMovie")
    public String findIdChartMovie(MovieChart movieChart,Model model){
        MovieChart findChartService = movieChartService.findById(movieChart.getShow_id());
        model.addAttribute("MovieChartList",findChartService);
        return "/admin/updataChartMovie";
    }

    @RequestMapping("/updataChartMovie")
    public  String updataChartMovie(MovieChart movieChart){
        movieChartService.updataChartMovie(movieChart);
        System.out.println(movieChart+"+++++++++++++++++++++movieChart+++++++++++++++++++++++");
        return "forward:/admin/showMovieChart";
    }



}
